﻿using STHPMS1.Common.C_Enum;
using STHPMS1.DAL.D_Core;
using STHPMS1.ICore.I_DAL.Repository;
using System.Collections.Generic;
using STHPMS1.Common.C_Extension;
using System.Data;
using ServiceStack.OrmLite;
using System.Linq.Expressions;
using System;
using STHPMS1.ICore.I_Entity.Stmpms1;
using ServiceStack.OrmLite.SqlServer;
using System.Data.SqlClient;

namespace STHPMS1.DAL.D_Repository
{
    public class B_PmstcRepository : DalBase, IB_PmstcRepository
    {
        public string GetDEGDF(double start, double end, string rdid)
        {
            using (SqlConnection conn = new SqlConnection(Adapter.ConnectionString))
            {
                string sql = string.Format(@"select DEGDF from B_PMSTC where RDWAYID='{0}' and (BEGINMP<={1} and {2}<=ENDMP)", rdid, start, end);
                conn.Open();
                SqlCommand cmmd = new SqlCommand(sql, conn);
                object obj = cmmd.ExecLongScalar();
                conn.Close();
                if (obj != null)
                    return obj.ToString();
                return "";
            }
        }
        public IEnumerable<T> GetPmstcThick<T>(int pvmttype)
              where T : class
        {
            string sql = string.Format(@"select RDWAYID,BEGINMP,ENDMP,SUM(THICK) THICK 
                                        from B_PMSTC 
                                        where (LAYER_DICTID={0} or LAYER_DICTID={1} or LAYER_DICTID={2})  and PVMTTYPE_DICTID={3}  
                                        GROUP BY RDWAYID,BEGINMP,ENDMP 
                                        ORDER BY BEGINMP ;
                                        ", LayerEnum.L30.GetEnumValue(), LayerEnum.L45.GetEnumValue(), LayerEnum.L50.GetEnumValue(), pvmttype);
            IDbConnection conn = Adapter.OpenDbConnection();
            return conn.Select<T>(sql);
        }

        public DataTable GetStruMates(string ids)
        {
            string sql = string.Format(@"SELECT b.items id,a.poirat,a.elamod,a.draincoeff,a.layercoeff FROM
(select * from B_STRU_MATE where STRU_ID in ( {0} )) a
right JOIN
(SELECT * FROM dbo.split('{1}',',')) b
on a.stru_id=b.items", ids, ids.Replace("'", "").Replace(" ","").Trim());
            return Adapter.DataAdapter.FillDataTable(sql);
        }

        public DataTable GetEmptyData()
        {
            return Adapter.DataAdapter.FillDataTable("select * from B_PMSTC where 1=2");
        }

        public DataTable GetLayerEmptyData()
        {
            return Adapter.DataAdapter.FillDataTable("select * from B_LAYER where 1=2");
        }

      
    }
}
